﻿using System.ComponentModel;
using JSon = System.Text.Json.JsonSerializer;
namespace Lib.WPF.Core;
/// <summary>
/// PropertyChanged 基类
/// </summary>
public class PropertyChangedBase : INotifyPropertyChanged, IDisposable {
    /// <summary>
    /// 
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged;
    /// <summary>
    /// 
    /// </summary>
    public void OnPropertyChanged(string PropertyName = null) {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
    }
    /// <summary>
    /// 当前属性值集合
    /// </summary>
    public Dictionary<string, object> Propertys = [];
    /// <summary>
    /// 历史属性值集合
    /// </summary>
    public Dictionary<string, object> Historys = [];
    /// <summary>
    /// 获取指定属性名的当前属性值
    /// </summary>
    public T GetProperty<T>(string Key) {
        if (string.IsNullOrEmpty(Key) || !Propertys.TryGetValue(Key, out object Value)) {
            return default;
        }
        return (T)Value;
    }
    /// <summary>
    /// 获取指定属性名的历史属性值
    /// </summary>
    public T GetHistory<T>(string Key) {
        if (string.IsNullOrEmpty(Key) || !Historys.TryGetValue(Key, out object Value)) {
            return default;
        }
        if (Value != null) {
            return (T)Value;
        }
        return default;
    }

    /// <summary>
    /// 
    /// </summary>
    public T GetPropertyValue<T>(string Key) {
        if (Propertys.TryGetValue(Key, out object Value)) {
            if(Value != null) {
                return (T)Value;
            }
        } else {
            Propertys.Add(Key, default);
        }
        return default;
    }
    /// <summary>
    /// 
    /// </summary>
    public void SetPropertyValue<T>(string Key, T Value) {
        if (!Propertys.TryGetValue(Key, out object Result) || Result != (object)Value) {
            Historys[Key] = Result;
            Result = Value;
            Propertys[Key] = Result;
            OnPropertyChanged(Key);
        }
    }
    /// <summary>
    /// 
    /// </summary>
    public void Dispose() {
        Propertys?.Clear();
    }
}